package com.dao.impl;

import com.dao.StudentDao;
import com.entity.Student;
import com.entity.StudentExample;
import com.mapper.StudentMapper;
import com.page.PageInfo;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("studentDao")
public class StudentDaoImp implements StudentDao {
    @Resource
    StudentMapper studentMapper;
    public List<Student> getAll() throws Exception{
        return studentMapper.getAll();
    }

    @Override
    public int addStudent(Student student) throws Exception {
        return studentMapper.insertSelective(student);
    }
    //如果在mycat 缓存管理器中 找得到 和cacheid匹配缓存，只查缓存 不执行dao的方法
    @Cacheable(value = "myCache",key = "#cacheId")
    public List<Student> getStudentsByPage(PageInfo pageInfo,String cacheId) throws Exception{
        System.out.println("不执行缓存");
        System.out.println("第"+pageInfo.getCurrPage()+"页可能走缓存");
        //将用户选择 当前页码和每页大小传入给map
        int currPage = pageInfo.getCurrPage();
        int pageSize = pageInfo.getPageSize();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", currPage);
        map.put("pageSize", pageSize);
        System.out.println(pageInfo.getCurrPage() + " "+ pageInfo.getPageSize());
        //调用示例查询得到当前的总记录数
        pageInfo.setItemCount((int) studentMapper.countByExample(new StudentExample()));
        return studentMapper.getStudentsByPage(map);
    }

    //如果在mycat 缓存管理器中 找得到 和cacheid匹配缓存，只查缓存 不执行dao的方法
    public List<Student> getStudentsByPage(PageInfo pageInfo) throws Exception{
        System.out.println("第"+pageInfo.getCurrPage()+"页不走缓存");
        //将用户选择 当前页码和每页大小传入给map
        int currPage = pageInfo.getCurrPage();
        int pageSize = pageInfo.getPageSize();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", currPage);
        map.put("pageSize", pageSize);
        System.out.println(pageInfo.getCurrPage() + " "+ pageInfo.getPageSize());
        //调用示例查询得到当前的总记录数
        pageInfo.setItemCount((int) studentMapper.countByExample(new StudentExample()));
        return studentMapper.getStudentsByPage(map);
    }
    @Cacheable(value = "myCache",key = "#cacheId")
    public int getItemCount(String cacheId){
        return (int)studentMapper.countByExample(new StudentExample());
    }


    public List<Student> searchStudentByName(String name,PageInfo pageInfo) throws Exception{
        StudentExample studentExample = new StudentExample();
        int limit = pageInfo.getPageSize();
        int offset = (pageInfo.getCurrPage() - 1) * limit;
        studentExample.setOffset(offset);//就是原来的firstResult 就是通过（currPage - 1） * pageSize
        studentExample.setLimit(limit);
        //criteria条件查询接口，添加条件

        studentExample.createCriteria().andNameLike(name);
        pageInfo.setItemCount((int)studentMapper.countByExample(studentExample));
      return studentMapper.selectByExample(studentExample);
    }

    public Student selectStudentWithScsAndCourse(String sno)throws Exception{
        return studentMapper.selectStudentWithScsAndCourse(sno);
    }

    public Student selectStudentBySno(String sno){
        return studentMapper.selectByPrimaryKey(sno);
    }

    public int updateStudentByCno(Student student){
        return studentMapper.updateByPrimaryKeySelective(student);
    }

    @Override
    public Student loginStudentByName(String name) {
        // TODO Auto-generated method stub
        return studentMapper.loginStudentByName(name);
    }
}
